﻿/* File: DataBase.cs
 * Summary: This file contains methods connecting 
 * client data to the remote web-based database
 * 
 * Author: Farzam Deed
 * */

using System;
using System.Net;
using System.Web;
using System.Collections;
using System.IO;
 
namespace WebRequestExample
{
 
    class WebPostRequest
    {
        WebRequest theRequest;
        HttpWebResponse theResponse;
        ArrayList  theQueryData;
 
        public WebPostRequest(string url)
        {
            theRequest = WebRequest.Create(url);
            theRequest.Method = "POST";
            theQueryData = new ArrayList();
        }

        //testing
        public WebPostRequest(String url, int nonsense)
        {
            theRequest = WebRequest.Create(url);
            theRequest.Method = "GET";
        }

 
        public void Add(string key, string value)
        {
            theQueryData.Add(String.Format("{0}={1}",key,HttpUtility.UrlEncode(value)));
        }

        public void ClearLocalData()
        {
            theQueryData.Clear();
        }
 
        public string GetResponse()
        {
            // Set the encoding type
            theRequest.ContentType="application/x-www-form-urlencoded";
 
            // Build a string containing all the parameters
            string Parameters = String.Join("&",(String[]) theQueryData.ToArray(typeof(string)));
            theRequest.ContentLength = Parameters.Length;
 
            // We write the parameters into the request
            StreamWriter sw = new StreamWriter(theRequest.GetRequestStream());
            sw.Write(Parameters);
            sw.Close();
 
            // Execute the query
            theResponse =  (HttpWebResponse)theRequest.GetResponse();
            StreamReader sr = new StreamReader(theResponse.GetResponseStream());
            return sr.ReadToEnd();
        }

        public int Timeout
        {
            get
            {
                return theRequest.Timeout;
            }
            set
            {
                theRequest.Timeout = value;
            }
        }

        //debugging method
        public void GetResponse(String HttpCmd)
        {
            if (HttpCmd == "GET")
            {
                const int buffer_size = 8192;
                //TODO get the webpage and store it to
                string fileName = "index.html";
                System.IO.FileStream fs;

                // Delete the file if it exists.
                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                    
                }

                fs = System.IO.File.Create(fileName, buffer_size);

                // used on each read operation
                byte[] buf = new byte[buffer_size];

                // prepare the web page we will be asking for

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                    theRequest.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                int count = 0;

                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // print out page to file;
                        fs.Write(buf, 0, count);
                    }
                }
                while (count > 0); // any more data to read?
                
            }
        }
 
    }
}